#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<string>
#include<stack>
using namespace std;
class Solution {
public:
    string decodeString(string s) {
        stack<string> as;
        as.push("");
        stack<int> aq;
        int i = 0;
        int n = s.size();
        while (i < n)
        {
            if (s[i] >= '0' && s[i] < '9')
            {
                int num = 0;
                while (s[i] >= '0' && s[i] <= '9')
                {
                    num = num * 10 + (s[i] - '0');
                    i++;
                }
                aq.push(num);
            }
            else if (s[i] == '[')
            {
                i++;
                string tmp;
                while (s[i] >= 'a' && s[i] <= 'z')
                {
                    tmp += s[i];
                    i++;
                }
                as.push(tmp);
            }
            else if (s[i] == ']')
            {
                int q = 0;
                string qw;
                q = aq.top();
                aq.pop();
                qw = as.top();
                as.pop();
                while (q)
                {
                    as.top() += qw;
                    q--;
                }
                i++;
            }
            else
            {
                while (i < n && s[i] >= 'a' && s[i] <= 'z')
                {
                    as.top() += s[i];
                    i++;
                }
            }
        }
        return as.top();
    }
};